<?php

namespace app\index\controller;

use app\common\controller\Front;
use app\common\logic\CacheKey;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

class About extends Front
{
    protected function _initialize()
    {
        parent::_initialize();

        $this->assign('aboutMenu', $this->getAboutMenu());
    }

    /**
     * 取得关于菜单
     * @return false|mixed|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function getAboutMenu()
    {
        $about_menu = cache(CacheKey::SINGLE_PAGE_MENU);
        if (!$about_menu) {
            $about_menu = Db::name('pages')->field('id,title')->order('id DESC')->select();
            cache(CacheKey::SINGLE_PAGE_MENU, $about_menu);
        }
        return $about_menu;
    }

    /**
     * @return mixed
     * @return false|mixed|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $id = $this->request->param('id', 1);
        if ($id < 1) $this->error('参数缺少！');

        $article = db('pages')->field('id,title,description,content')->where(['id' => $id])->order('id DESC')->find();


        $this->assign('article', $article);
        return $this->fetch();
    }


    public function tools()
    {
        return $this->fetch();
    }

    /**
     * @return mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function sitemap()
    {
        //导航
        $data = Db::name('channel')->select()->toArray();
        $this->assign('channel', $data);

        //分类
        $cate = Db::name('category')->select()->toArray();
        $this->assign('cate', $cate);

        return $this->fetch();
    }
}
